package com.catsic.web.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.core.autharea.service.AuthAreaService;
import com.catsic.core.form.AuthAreaForm;

@Controller
@RequestMapping("/authArea")
public class AuthAreaController extends BaseController {
	
	@Autowired
	private AuthAreaService authAreaService;
	
	/**
	 * create:添加
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(AuthAreaForm form){
		form = authAreaService.add(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(AuthAreaForm form){
		form = authAreaService.update(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		authAreaService.delete(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过父节点code获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/query" )
	@ResponseBody
	public Map<String,Object> query(String code){
		List<AuthAreaForm> list = authAreaService.query(code);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过资源节点code与Pid为空来获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/findByPidAndResourceCode" )
	@ResponseBody
	public Map<String,Object> findByPidAndResourceCode(String resourceCode){
		List<AuthAreaForm> list = authAreaService.findFormByPidIsNullAndResourceCodeOrderByCodeDesc(resourceCode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过资源节点code获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/findByResourceCode")
	@ResponseBody
	public Map<String,Object> findByResourceCode(String resourceCode){
		List<AuthAreaForm> list = authAreaService.findFormByResourceCode(resourceCode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
}
